/*
	---------------------------------------------
	-	JS Stuff
	-	/core.js
	-	Namespace, major scripts etc...
	---------------------------------------------
	-	Author:	James Haley
	-			jameshaley@sevendigits.com
	-			2011
	---------------------------------------------
*/
	
	var seven={}; // SevenDigits
	
	/*-- Flour -------------------- */
		
		//	++ Initiation
		//----------------------------------------------
		//	First run settings/animations
			
			seven.init=function() {
				// Load functions
				seven.memory.load();
				seven.watcher.assign();
				
				
				
				
				// Assign Actions
				$('oSetDefault').observe('click',seven.memory.setDefault);
				$('oInspectShow').observe('click',function(){var vBase = localStorage.defaultBase||'12px';chrome.tabs.executeScript(null,{code:"var nDefaultBase = '"+vBase+"';"});chrome.tabs.executeScript(null,{file:"scripts/p.js"});chrome.tabs.executeScript(null,{file:"scripts/context.js"});window.close();});
				$('oAboutShow').observe('click',function(){$('oMainPage').hide();$('oAboutPage').show();});
				$('oAboutClose').observe('click',function(){$('oAboutPage').hide();$('oMainPage').show();});
			};
		
		
		//	++ Utilities
		//----------------------------------------------
		
			(seven.utils = function() {
				return {
					rnd:function(nMax){
						nMax=nMax||1000000000;
						return Math.floor(Math.random()*nMax);
					}
				}
			}());
	
	
	/*-- Yeast -------------------- */
		
		//	++ localStorage
		//----------------------------------------------
		//	Check if first time/load previous figures
		
			(seven.memory=function(){
				return {
					load:function(){
						var bDefaultsSet=localStorage.emCalcDefaultsSet||false;
						if(bDefaultsSet){
							$('oPX').value=localStorage.defaultPX;
							$('oEM').value=localStorage.defaultEM;
							$('oBase').value=localStorage.defaultBase;
						} else {
							$('oPX').value='24px';
							$('oEM').value='2em';
							$('oBase').value='12px';
						}
					},
					setDefault:function(){
						localStorage.emCalcDefaultsSet=true;
						localStorage.defaultPX=$('oPX').value;
						localStorage.defaultEM=$('oEM').value;
						localStorage.defaultBase=$('oBase').value;
						
						// Update link to show updated
						$('oSetDefault').addClassName('done').update('Saved as default values!');
						window.setTimeout(function(){$('oSetDefault').removeClassName('done').update('Set as default values &raquo;');},5000);
					}
				}
			}());
			
		//	++ Calculations
		//----------------------------------------------------
		//	Observe the text fields and calculate live values
		
			(seven.watcher=function(){
				// Decimal places
				var _nDecimalPlaces=3; // Default
				// Calculate, private function
				var oCalculate = function(e){
					var nCurrentValue = e.findElement().value.tidyNumeric();
					console.log(nCurrentValue);
					if(isNaN(nCurrentValue)!==true){
						// Calculate required sum
						if(e.findElement().id=='oEM'){ // EM
							var nCurrentBase = $('oBase').value.tidyNumeric();
							$('oPX').value = (nCurrentValue*nCurrentBase).tidyDecimal(0)+'px';
						} else if(e.findElement().id=='oPX'){ // PX
							var nCurrentEM = $('oEM').value.tidyNumeric();
							var nCurrentBase = $('oBase').value.tidyNumeric();
							
							$('oEM').value = (nCurrentValue>0)?(nCurrentValue/nCurrentBase).tidyDecimal(3)+'em':0+'em';
						} else if(e.findElement().id=='oBase'){ // PX
							var nCurrentEM = $('oEM').value.tidyNumeric();
							var nCurrentPX = $('oPX').value.tidyNumeric();
							
							$('oEM').value = (nCurrentValue>0)?(nCurrentPX/nCurrentValue).tidyDecimal(3)+'em':0+'em';
						}
					} else {
						console.log('isnan');
					};
				};
				// Public functions
				return {
					assign:function(){
						// Key ups
						$('oPX').observe('keyup',function(e){oCalculate(e);});
						$('oEM').observe('keyup',function(e){oCalculate(e);});
						$('oBase').observe('keyup',function(e){oCalculate(e);});
						
						// Blur
						$('oPX').observe('blur',function(e){e.findElement().value=e.findElement().value.tidyNumeric().tidyDecimal(0)+'px';});
						$('oEM').observe('blur',function(e){e.findElement().value=e.findElement().value.tidyNumeric().tidyDecimal(3)+'em';});
						$('oBase').observe('blur',function(e){e.findElement().value=e.findElement().value.tidyNumeric().tidyDecimal(0)+'px';});
					}
				}
			}());

	/*-- Water -------------------- */
		
		//	++ Useful prototypes
		//----------------------------------------------
		//	It's a mixture
		
			// Remove last full stop prototype
			String.prototype.trimFullStops = function() {
				return this.replace(/^\.+|\.+$/,"");
			};
			
			// Make Numeric
			String.prototype.tidyNumeric = function() {
				return Math.abs(this.replace(/[^0-9.]/ig,'').trimFullStops());
			};
			
			// Tidy value
			Number.prototype.tidyDecimal = function(n) {
				return Math.abs(this.toFixed(n));
			}
			
						
	/*-- Bake -------------------- */
	
		//	++ Run Site
		//----------------------------------------------
			document.observe("dom:loaded",seven.init);
			
			